package com.android.aishangauction.fragment.member;


import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.aishangauction.R;
import com.android.aishangauction.adapt.BaseFragmentAdapter;
import com.android.aishangauction.fragment.BaseLazyFragment;
import com.android.aishangauction.model.member.ScoreDetailData;
import com.android.aishangauction.model.member.ScoreDetailDataCallback;
import com.android.aishangauction.utils.ConstantsUrl;
import com.android.aishangauction.utils.HttpUtils;
import com.android.aishangauction.utils.ObjectUtils;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * 积分明细页
 */
public class Fragment_Mine_Score_Detail extends BaseLazyFragment {

    //    @BindView(R.id.click_resetnetwork)
//    LinearLayout clickResetnetwork;
//    @BindView(R.id.progress)
//    RelativeLayout progress;
//    @BindView(R.id.no_data)
//    RelativeLayout noData;
//    @BindView(R.id.no_data_tv)
//    TextView noDataTv;
    @BindView(R.id.score_detail_total_score)
    TextView totalScore;
    @BindView(R.id.tv_tab_score_income)
    TextView tv_Income;
    @BindView(R.id.line_tab_score_income)
    View line_Income;
    @BindView(R.id.tv_tab_score_expenditure)
    TextView tv_Expenditure;
    @BindView(R.id.line_tab_score_expenditure)
    View line_Expenditure;
    @BindView(R.id.viewpager)
    ViewPager mViewPager;

    private FragmentActivity mcontext;
    private View view;
    private boolean networkConnected;
    private String explain;
    AlertDialog dialog;

    public Fragment_Mine_Score_Detail() {

    }

    public static Fragment_Mine_Score_Detail newInstance() {
        Fragment_Mine_Score_Detail fragment = new Fragment_Mine_Score_Detail();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initPrepare() {

    }

    @Override
    protected void onInvisible() {

    }

    @Override
    protected void initData() {
        getScoreData();
        setupViewPager();
    }

    @Override
    protected View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_mine_score_detail, container, false);
        mcontext = getActivity();
        ButterKnife.bind(this, view);
        return view;
    }


    public void getScoreData() {
        String address = ConstantsUrl.domain + ConstantsUrl.SCORE_DETAIL;
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        HashMap<String, String> map = new HashMap<>();
        map.put("page", "0");
        if (networkConnected) {
            HashMap<String, String> header = ObjectUtils.getHeader(mcontext);
            HttpUtils.get(address, header, map, dataCallback);
        } else {
//            ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
        }
    }

    ScoreDetailDataCallback dataCallback = new ScoreDetailDataCallback() {

        @Override
        public void onError(Call call, Exception e, int id) {
//            ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
        }

        @Override
        public void onResponse(ScoreDetailData response, int id) {
//            ObjectUtils.GetDataNet(clickResetnetwork, progress, 1);
            if (response.getCode() == 0) {
                totalScore.setText(response.getTotal_left());
                explain = response.getExplain();
            } else {
//                noData.setVisibility(View.VISIBLE);
//                noDataTv.setText(response.getMsg());
//                noDataTv.setVisibility(View.VISIBLE);
            }
        }
    };

    private void setupViewPager() {

        ArrayList<BaseLazyFragment> mFragments = new ArrayList<>();
        Fragment_Mine_Score_Detail_Tab fragment1 = Fragment_Mine_Score_Detail_Tab.newInstance("1");
        Fragment_Mine_Score_Detail_Tab fragment2 = Fragment_Mine_Score_Detail_Tab.newInstance("2");
        mFragments.add(fragment1);
        mFragments.add(fragment2);

        BaseFragmentAdapter adapter =
                new BaseFragmentAdapter(getActivity().getSupportFragmentManager(), mFragments);

        mViewPager.setAdapter(adapter);
    }

    @OnClick({R.id.score_detail_explain,
            R.id.rl_tab_score_income, R.id.rl_tab_score_expenditure
    })
    public void onClick(View view) {
        switch (view.getId()) {
            //积分说明
            case R.id.score_detail_explain:
                if (explain != null && !explain.equals("")) {
                    showDialog();
                }
                break;
            //收入明细
            case R.id.rl_tab_score_income:
                tv_Income.setTextColor(Color.RED);
                tv_Expenditure.setTextColor(Color.BLACK);
                line_Income.setVisibility(View.VISIBLE);
                line_Expenditure.setVisibility(View.INVISIBLE);
                mViewPager.setCurrentItem(0);
                break;
            //支出明细
            case R.id.rl_tab_score_expenditure:
                tv_Expenditure.setTextColor(Color.RED);
                tv_Income.setTextColor(Color.BLACK);
                line_Expenditure.setVisibility(View.VISIBLE);
                line_Income.setVisibility(View.INVISIBLE);
                mViewPager.setCurrentItem(1);
                break;
        }
    }

    private void showDialog() {
        LayoutInflater inflater = (LayoutInflater) mcontext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.dialog_score_explain, null);
        TextView sure = (TextView) v.findViewById(R.id.dialog_sure);
        TextView content = (TextView) v.findViewById(R.id.dialog_content);
        content.setText(Html.fromHtml(explain));
        sure.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        dialog.dismiss();
                                    }
                                }
        );
        dialog = new AlertDialog.Builder(mcontext).setView(v).setCancelable(true).show();
    }
}
